﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OL = Microsoft.Office.Interop.Outlook;
using Microsoft.Win32;
using System.Runtime.InteropServices;
using System.Collections;


namespace CDCOSDH
{
    public partial class JournalizeSingle : Form
    {
        private OL.MailItem mailItem;
        private OL.ReportItem reportItem;
        private OL.MeetingItem meetingItem;
        private OL.Recipients recipients;

        private object selectedObject;
        private List<OL.MailItem> selectedObjectList;

        private const string LESS_THAN = "<";
        private const string GREATER_THAN = ">";
        private const string SPACE = " ";

        private string mailRightSelected = string.Empty;
        private string freeCatsSelected = string.Empty;
        private string caseAreaSelected = string.Empty;
        private string casePartsSelected = string.Empty;
        private string caseParts = string.Empty;

        private List<string> recpts = new List<string>(1000);

        private delegate void delegateUpdateSetCaseNum(string message);

        public JournalizeSingle(object selectedItem)
        {
            this.selectedObject = selectedItem;
            InitializeComponent();
        }

        private void btnCaseArea_Click(object sender, EventArgs e)
        {
            using (SubjectHeading subjectHeading = new SubjectHeading(this.lblCaseArea.Text))
            {
                if (subjectHeading.ShowDialog(this) == DialogResult.OK)
                {

                }
            }

        }

        private void JournalizeSingle_Load(object sender, EventArgs e)
        {
            if (this.selectedObject is OL.MailItem)
            {
                this.mailItem = (OL.MailItem)this.selectedObject;
                recipients = mailItem.Recipients;

                #region
                this.setSubjectTpTxt();

                this.setEmailToTptxt();

                this.cbSDH.DataSource = ConvergensDominoConnector.DominoConnector.Default.FindSDHDBS();
                this.setLastSelectedSDH();

                this.cbCaseOfr.DataSource = ConvergensDominoConnector.DominoConnector.Default.LoadCaseOfficers();
                this.cbCaseOfr.SelectedItem = ConvergensDominoConnector.DominoConnector.Default.CommonName;

                this.cbRplyReq.SelectedIndex = 0;
                #endregion

                if (this.mailItem.ReceivedByName == null)
                {
                    this.btnExpandMailTo.Visible = false;
                    this.btnExpandSubject.Visible = false;
                    this.btnExpandMailRights.Visible = false;
                    this.btnCaseParts.Visible = false;

                    this.pictureBox4.Visible = false;
                    this.lblMailInvitRlpy.Visible = false;
                    this.cbRplyReq.Visible = false;

                    this.lblMailDirection.Text = "Udgående e-mail";
                    this.lblMailDir.Text = "E-mail til";
                    this.tbDocName.Text = this.mailItem.Subject;
                    this.btnFndRlpySeqNum.Visible = true;
                    this.btnFndRlpySeqNum.Enabled = true;
                    this.pictureBox3.Visible = true;
                    this.lblSeqNum.Visible = true;

                    this.pbRlpyMail.Visible = true;

                    this.lblMailToFromTxt.Text = this.mailItem.To;

                    this.lblSeqNum.Visible = true;
                    this.pbRlpyMail.Visible = true;
                    this.lblRlplyMailInvitOutGoing.Visible= true;
                    this.rbYes.Visible = true;
                    this.rbNo.Visible = true;
                    this.btnFndRlpySeqNum.Enabled = true;
                    this.tbSeqNum.Enabled = true;


                    this.lblSeqNum.Enabled = true;
                    this.lblRlplyMailInvitOutGoing.Enabled = true;
                    this.rbYes.Enabled = true;
                    this.rbNo.Enabled = true;
                    this.btnFndRlpySeqNum.Enabled = true;
                    this.tbSeqNum.Enabled = true;

                    if (this.mailItem.Subject.Length > 75)
                    {
                        this.btnExpandSubject.Visible = true;
                        this.lblMailSubjectValue.Text = this.mailItem.Subject.Substring(0, 75);
                        this.tpSubject.SetToolTip(this.btnExpandSubject, this.mailItem.Subject);
                    }
                    else
                    {
                        this.btnExpandSubject.Visible = false;
                        this.lblMailSubjectValue.Text = this.mailItem.Subject.Substring(0, this.mailItem.Subject.Length);
                        this.tpSubject.SetToolTip(this.btnExpandSubject, this.mailItem.Subject);
                    }

                    if (this.mailItem.To != null)
                    {
                        //&& this.mailItem.To.Contains(';')
                        this.btnExpandMailTo.Visible = true;

                        string toolTipText = string.Empty;

                        if (this.mailItem.To.Length >= 55)
                        {
                            toolTipText = this.mailItem.To.Replace(";", Environment.NewLine);
                            this.tpEmailTo.SetToolTip(this.btnExpandMailTo, toolTipText);
                        }
                        else
                        {
                            this.btnExpandMailTo.Visible = false;
                            this.tpEmailTo.SetToolTip(this.btnExpandMailTo, toolTipText);
                        }

                    }
                }
                else
                {
                    if (this.recipients.Count > 0)
                    {
                        for (int i = 1; i <= this.recipients.Count; i++)
                        {
                            OL.Recipient recipient = recipients[i];
                            if (recipient.Type == 1)
                            {
                                OL.AddressEntry addressEntry = this.recipients[i].AddressEntry;
                                if (!addressEntry.Address.Contains("@"))
                                {
                                    recpts.Add(addressEntry.Name + SPACE + LESS_THAN + ConvergensDominoConnector.DominoConnector.Default.GetSMTPAddress(addressEntry.Address) + GREATER_THAN);
                                }
                                else
                                {
                                    recpts.Add(addressEntry.Name + SPACE + LESS_THAN + addressEntry.Address + GREATER_THAN);
                                }
                            }

                        }
                    }

                    this.btnExpandMailTo.Visible = false;

                    this.lblSeqNum.Visible = false;
                    this.lblRlplyMailInvitOutGoing.Visible = false;
                    this.rbYes.Visible = false;
                    this.rbNo.Visible = false;
                    this.btnFndRlpySeqNum.Visible = false;
                    this.tbSeqNum.Visible = false;
                    this.btnCaseParts.Visible = false;

                    this.pictureBox4.Visible = true;
                    this.lblMailInvitRlpy.Visible = true;
                    this.cbRplyReq.Visible = true;

                    this.lblMailDir.Text = "E-mail til";
                    this.lblMailDirection.Text = "Indgående e-mail";
                    this.lblMailToFromTxt.Text = string.Join(", ", recpts.ToArray());
                    this.lblMailSubjectValue.Text = mailItem.Subject;
                    this.tbDocName.Text = this.mailItem.Subject;
                    this.lblSDHname.Text = this.cbSDH.Text;
                    this.btnFndRlpySeqNum.Enabled = false;
                }

            }
            else if (this.selectedObject is OL.ReportItem)
            {
                this.reportItem = (OL.ReportItem)this.selectedObject;
                this.mailItem.Subject = this.reportItem.Subject;
            }
            else if (this.selectedObject is OL.MeetingItem)
            {
                this.meetingItem = (OL.MeetingItem)this.selectedObject;
                OL.Application application = new OL.Application();
                OL.MailItem mailproxy = application.CreateItem(OL.OlItemType.olMailItem) as OL.MailItem;

                this.mailItem = mailproxy;
                this.mailItem.Subject = this.meetingItem.Subject;
                recipients = meetingItem.Recipients;
                application = null;
                //Marshal.ReleaseComObject(application);
                //Marshal.ReleaseComObject(mailproxy);
            }
        }

        #region getters and setters
        public string tbSeqNumTxt
        {
            get { return this.tbSeqNum.Text; }
            set { tbSeqNum.Text = value; }
        }

        public string tbCaseNumTxt
        {
            get { return this.tbCaseNum.Text; }
            set { tbCaseNum.Text = value; }
        }

        public string lbCaseSubjectTxt
        {
            get { return this.lbtitle.Text; }
            set { lbtitle.Text = value; }
        }

        public string cbSDHTxt
        {
            get { return this.lblSDHname.Text; }
            set { lblSDHname.Text = value; }
        }

        public string cbRlpyReqTxt
        {
            get { return this.cbRplyReq.SelectedItem.ToString(); }
            set { cbRplyReq.Text = value; }
        }

        public string cbCaseOfrTxt
        {
            get { return this.cbCaseOfr.Text; }
            set { cbCaseOfr.Text = value; }
        }
        
        public string tbDocNameTxt
        {
            get { return this.tbDocName.Text; }
            set { tbDocName.Text = value; }
        }

        public string MailRightSelectedTxt
        {
            get { return this.mailRightSelected; }
            set { mailRightSelected = value; }
        }

        public string FreeCatsSelectedTxt
        {
            get { return this.freeCatsSelected; }
            set { freeCatsSelected = value; }
        }

        public string tbRemarksTxt
        {
            get { return this.tbRemarks.Text; }
            set { tbRemarks.Text = value; }
        }

        public string cbCasePartsTxt
        {
            get { return this.lbCasePartsSelected.Text; }
            set { lbCasePartsSelected.Text = value; }
        }

        public string lbFreeCatTxt
        {
            get { return this.lbFreeCat.Text; }
            set { lbFreeCat.Text = value; }
        }

        public string lbCaseAreaTxt
        {
            get { return this.lbCaseArea.Text; }
            set { lbCaseArea.Text = value; }
        }

        public string lbEdctionTxt
        {
            get { return this.lbEdction.Text; }
            set { lbEdction.Text = value; }
        }

        public string lbInstitutionTxt
        {
            get { return this.lbInstitution.Text; }
            set { lbInstitution.Text = value; }
        }

        public string lbLeglAspctsTxt
        {
            get { return this.lbLeglAspcts.Text; }
            set { lbLeglAspcts.Text = value; }
        }

        public string lbEcoAspctsTxt
        {
            get { return this.lbEcoAspcts.Text; }
            set { lbEcoAspcts.Text = value; }
        }

        public bool cbOpenCaseValue 
        {
            get { return this.cbOpenCase.Checked; }
            set { cbOpenCase.Checked = value; }
        }
        #endregion

        private void cbSDH_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.tbCaseNum.Text = string.Empty;
            this.lblSDHname.Text = this.cbSDH.Text;
         
            if (ConvergensDominoConnector.DominoConnector.Default.GetSDHEmneOrdJaNej(this.cbSDH.Text) == "Ja")
            {
                this.switchEnmeordVariant2();
            }
            else
            {
                this.switchEnmeordVariant1();
            }
        }

        private void switchEnmeordVariant1()
        {
            this.lbEdction.Visible = true;
            this.lblEdction.Visible = true;

            this.lbInstitution.Visible = true;
            this.lblInstitution.Visible = true;

            this.lbLeglAspcts.Visible = true;
            this.lblLeglAspcts.Visible = true;

            this.lbEcoAspcts.Visible = true;
            this.lblEcoAspcts.Visible = true;

            this.lbCaseArea.Text = string.Empty;
            this.lbEdction.Text = string.Empty;
            this.lbInstitution.Text = string.Empty;
            this.lbLeglAspcts.Text = string.Empty;
            this.lbEcoAspcts.Text = string.Empty;
            this.lbCaseAreaSelected.Text = string.Empty;
            this.lbtitle.Text = string.Empty;

            this.caseAreaSelected = string.Empty;

            this.lbFreeCatsSelected.Text = string.Empty;

            this.btnGetCaseArea.Visible = false;
            this.btnExpandCaseArea.Visible = false;
            this.btnExpandEdction.Visible = false;
            this.btnExpandInstitution.Visible = false;
            this.btnExpandLeglAspcts.Visible = false;
            this.btnExpandEcoAspcts.Visible = false;
            this.btnExpandFreeCats.Visible = false;
        }

        private void switchEnmeordVariant2()
        {
            this.lbEdction.Visible = false;
            this.lblEdction.Visible = false;

            this.lbInstitution.Visible = false;
            this.lblInstitution.Visible = false;

            this.lbLeglAspcts.Visible = false;
            this.lblLeglAspcts.Visible = false;

            this.lbEcoAspcts.Visible = false;
            this.lblEcoAspcts.Visible = false;

            this.lbCaseArea.Text = string.Empty;
            this.lbEdction.Text = string.Empty;
            this.lbInstitution.Text = string.Empty;
            this.lbLeglAspcts.Text = string.Empty;
            this.lbEcoAspcts.Text = string.Empty;
            this.lbCaseAreaSelected.Text = string.Empty;
            this.lbtitle.Text = string.Empty;
            this.caseAreaSelected = string.Empty;

            this.lbFreeCatsSelected.Text = string.Empty;

            this.btnGetCaseArea.Visible = true;

            this.btnExpandCaseArea.Visible = false;
            this.btnExpandEdction.Visible = false;
            this.btnExpandInstitution.Visible = false;
            this.btnExpandLeglAspcts.Visible = false;
            this.btnExpandEcoAspcts.Visible = false;
            this.btnExpandFreeCats.Visible = false;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void btnExpandMailTo_Click(object sender, EventArgs e)
        {
            this.setEmailToTptxt();
        }

        private void btnExpandSubject_Click(object sender, EventArgs e)
        {
            this.setSubjectTpTxt();
        }

        private RegistryKey CreateRegistryKey()
        {
            RegistryKey registryKey = Registry.CurrentUser;
            if (registryKey == null)
            {
                throw new Exception("Failed to open current user registry key.");
            }
            registryKey = registryKey.CreateSubKey("Software");
            if (registryKey == null)
            {
                throw new Exception("Failed to create registry key. " + registryKey.ToString() + "/" + "Software");
            }
            registryKey = registryKey.CreateSubKey("Convergens");
            if (registryKey == null)
            {
                throw new Exception("Failed to create registry key. " + registryKey.ToString() + "/" + "Convergens");
            }
            registryKey = registryKey.CreateSubKey("CDCOSDH");
            if (registryKey == null)
            {
                throw new Exception("Failed to create registry key. " + registryKey.ToString() + "/" + "CDCOSDH");
            }
            return registryKey;
        }

        private RegistryKey OpenRegistryKey()
        {
            RegistryKey registryKey = Registry.CurrentUser;
            if (registryKey == null)
            {
                throw new Exception("Failed to open current user registry key.");
            }
            registryKey = registryKey.OpenSubKey("Software");
            if (registryKey == null)
            {
                throw new Exception("Failed to open registry key. " + registryKey.ToString() + "/" + "Software");
            }
            registryKey = registryKey.OpenSubKey("Convergens");
            if (registryKey == null)
            {
                throw new Exception("Failed to open registry key. " + registryKey.ToString() + "/" + "Convergens");
            }
            registryKey = registryKey.OpenSubKey("CDCOSDH");
            if (registryKey == null)
            {
                throw new Exception("Failed to open registry key. " + registryKey.ToString() + "/" + "CDCOSDH");
            }
            return registryKey;
        }

        private void btnOk_Click(object sender, EventArgs e) ///XXX.XXX.XXX
        {
            if (this.tbCaseNum.Text.Length != 11)
            {
                MessageBox.Show("Der skal angives et sagsnummer inden du kan trykke OK.", "Convergens Domino Connector for SDH", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            if (this.rbYes.Checked & this.tbSeqNum.Text.Length != 11 & this.mailItem.ReceivedByName == null)
            {
                MessageBox.Show("Svarløbenummer er ikke udfyldt korrekt.", "Convergens Domino Connector for SDH", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            RegistryKey registryKey = CreateRegistryKey();

            try
            {
                registryKey.SetValue("LastCbSDHDb", this.cbSDH.Text, RegistryValueKind.String);
                registryKey.Flush();
                registryKey.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            this.DialogResult = DialogResult.OK;
        }

        private void btnMailRights_Click(object sender, EventArgs e)
        {
            using (FrmListbox fListBox = new FrmListbox("hvem må se e-mailen i SDH", this.mailRightSelected.Split(';')))
            {
                this.mailRightSelected = string.Empty;

                if (fListBox.ShowDialog(this) == DialogResult.OK)
                {
                    for (int i = 0; i < fListBox.listView.CheckedItems.Count; i++)
                    {
                        if (i == 0)
                        {
                            this.mailRightSelected = fListBox.listView.CheckedItems[i].Text;
                        }
                        else
                        {
                            this.mailRightSelected = this.mailRightSelected + ";" + fListBox.listView.CheckedItems[i].Text;
                        }
                    }

                    this.setMailRightsTpTxt(this.mailRightSelected);
                }
            }
        }

        private void btnExpandMailRights_Click(object sender, EventArgs e)
        {
            this.setMailRightsTpTxt(this.mailRightSelected);
        }

        private void setEmailToTptxt()
        {
            string toolTipText = string.Empty;

            if (this.recpts.ToString().Length >= 47)
            {
                if (this.recpts.Count > 1)
                {
                    this.btnExpandMailTo.Visible = true;

                    for (int i = 0; i < this.recpts.Count; i++)
                    {
                        toolTipText += this.recpts[i].ToString();
                        toolTipText += Environment.NewLine;
                    }

                    this.tpEmailTo.SetToolTip(this.btnExpandMailTo, toolTipText);
                }
                else
                {
                    toolTipText = this.mailItem.To.Replace(";", Environment.NewLine);
                    this.tpEmailTo.SetToolTip(this.btnExpandMailTo, toolTipText);
                }

            }
            else
            {
                this.btnExpandMailTo.Visible = false;
                this.tpEmailTo.SetToolTip(this.btnExpandMailTo, toolTipText);
            }

        }

        private void setSubjectTpTxt()
        {
            if (this.mailItem.Subject.Length > 55)
            {
                this.btnExpandSubject.Visible = true;
                this.lblMailSubjectValue.Text = this.mailItem.Subject.Substring(0, 55);
                this.tpSubject.SetToolTip(this.btnExpandSubject, this.mailItem.Subject);
            }
            else
            {
                this.btnExpandSubject.Visible = false;
                this.lblMailSubjectValue.Text = this.mailItem.Subject.Substring(0, this.mailItem.Subject.Length);
                this.tpSubject.SetToolTip(this.btnExpandSubject, this.mailItem.Subject);
            }
        }

        private void setFreeCatsTpTxt(string freeCatsSelected)
        {
            string toolTipText = freeCatsSelected;

            this.lbFreeCatsSelected.Text = freeCatsSelected;

            if (freeCatsSelected.Length > 55)
            {
                this.btnExpandFreeCats.Visible = true;
                this.lbFreeCatsSelected.Text = this.lbFreeCatsSelected.Text.Substring(0, 55);
                this.tpFreeCats.SetToolTip(this.btnExpandFreeCats, toolTipText.Replace(";", Environment.NewLine));
            }
            else
            {
                this.btnExpandFreeCats.Visible = false;
                this.tpFreeCats.SetToolTip(this.btnExpandFreeCats, toolTipText);
            }

        }

        private void setCaseAreaTpTxt(string caseAreaSelected)
        {
            string toolTipText = caseAreaSelected;

            this.lbCaseAreaSelected.Text = caseAreaSelected;

            if (caseAreaSelected.Length > 75)
            {
                this.btnExpandCaseArea.Visible = true;
                this.lbCaseAreaSelected.Text = this.lbCaseAreaSelected.Text.Substring(0, 75);
                this.tpCaseArea.SetToolTip(this.btnExpandCaseArea, toolTipText.Replace(";", Environment.NewLine));
            }
            else
            {
                this.btnExpandCaseArea.Visible = false;
                this.tpCaseArea.SetToolTip(this.btnExpandCaseArea, toolTipText);
            }

        }

        private void setMailRightsTpTxt(string mailRightSelected)
        {
            string toolTipText = mailRightSelected;

            this.lbMailRightSelected.Text = mailRightSelected;

            if (mailRightSelected.Length > 55)
            {
                this.btnExpandMailRights.Visible = true;
                this.lbMailRightSelected.Text = this.lbMailRightSelected.Text.Substring(0, 55);
                this.tpMailRights.SetToolTip(this.btnExpandMailRights, toolTipText.Replace(";", Environment.NewLine));
            }
            else
            {
                this.btnExpandMailRights.Visible = false;
                this.tpMailRights.SetToolTip(this.btnExpandMailRights, toolTipText);
            }

        }

        private void setCaseAreaTpTxt()
        {
            string toolTipText = this.lbCaseArea.Text;

            if (this.lbCaseArea.Text.Length > 75)
            {
                this.btnExpandCaseArea.Visible = true;
                this.tpCaseArea.SetToolTip(this.btnExpandCaseArea, toolTipText.Replace(";", Environment.NewLine));
            }
            else
            {
                this.btnExpandCaseArea.Visible = false; 
                this.tpCaseArea.SetToolTip(this.btnExpandCaseArea, toolTipText);
            }
        }

        private void setEdctionTpTxt() 
        {
            string toolTipText = this.lbEdction.Text;

            if (this.lbEdction.Text.Length > 75)
            {
                this.btnExpandEdction.Visible = true;
                this.tpEdction.SetToolTip(this.btnExpandEdction, toolTipText.Replace(";", Environment.NewLine));
            }
            else
            {
                this.btnExpandEdction.Visible = false; 
                this.tpEdction.SetToolTip(this.btnExpandEdction, toolTipText);
            }
 
        }

        private void setInstitutionTpTxt()
        {
            string toolTipText = this.lbInstitution.Text;

            if (this.lbInstitution.Text.Length > 75)
            {
                this.btnExpandInstitution.Visible = true;
                this.tpInstitution.SetToolTip(this.btnExpandInstitution, toolTipText.Replace(";", Environment.NewLine));
            }
            else
            {
                this.btnExpandInstitution.Visible = false;
                this.tpInstitution.SetToolTip(this.btnExpandInstitution, toolTipText);
            }

        }

        private void setLeglAspctsTpTxt()
        {
            string toolTipText = this.lbLeglAspcts.Text;

            if (this.lbLeglAspcts.Text.Length > 75)
            {
                this.btnExpandLeglAspcts.Visible = true;
                this.tpLeglAspcts.SetToolTip(this.btnExpandLeglAspcts, toolTipText.Replace(";", Environment.NewLine));
            }
            else
            {
                this.btnExpandLeglAspcts.Visible = false;
                this.tpLeglAspcts.SetToolTip(this.btnExpandLeglAspcts, toolTipText);
            }

        }

        private void setEcoAspctsTpTxt()
        {
            string toolTipText = this.lbEcoAspcts.Text;

            if (this.lbEcoAspcts.Text.Length > 75)
            {
                this.btnExpandEcoAspcts.Visible = true;
                this.tpEcoAspcts.SetToolTip(this.btnExpandEcoAspcts, toolTipText.Replace(";", Environment.NewLine));
            }
            else
            {
                this.btnExpandEcoAspcts.Visible = false;
                this.tpEcoAspcts.SetToolTip(this.btnExpandEcoAspcts, toolTipText);
            }
        }

        private void setLastSelectedSDH()
        {
            try
            {
                //Select the RegistryHive you wish to read from 
                RegistryKey key = RegistryKey.OpenRemoteBaseKey(RegistryHive.CurrentUser, "");

                //Select the path within the hive 
                RegistryKey subkey = key.OpenSubKey("Software\\Convergens\\CDCOSDH");

                if (subkey != null)
                {
                    //Read the key 
                    this.cbSDH.SelectedItem = subkey.GetValue("LastCbSDHDB");
                }

                //RegistryKey registryKey = OpenRegistryKey();
                //if (Application.UserAppDataRegistry.GetValue("LastCbSDHDb") != null)
                //{
                //    object objLastCbSDHDB = registryKey.GetValue("LastCbSDHDB");
                //    if (objLastCbSDHDB != null)
                //    {
                //        RegistryValueKind lastCbSDHDbValueKind = registryKey.GetValueKind("LastCbSDHDB");
                //        switch (lastCbSDHDbValueKind)
                //        {
                //            case RegistryValueKind.DWord:
                //                break;
                //            case RegistryValueKind.Binary:
                //                break;
                //            case RegistryValueKind.ExpandString:
                //                this.cbSDH.SelectedItem = (string)objLastCbSDHDB;
                //                break;
                //            case RegistryValueKind.MultiString:
                //                this.cbSDH.SelectedItem = (string)objLastCbSDHDB;
                //                break;
                //            case RegistryValueKind.QWord:
                //                break;
                //            case RegistryValueKind.String:
                //                this.cbSDH.SelectedItem = (string)objLastCbSDHDB;
                //                break;
                //            case RegistryValueKind.Unknown:
                //            default:
                //                break;
                //        }
                //    }
                //}
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnFndCaseNum_Click(object sender, EventArgs e)
        {
            using (FindCaseNum findCaseNum = new FindCaseNum(this.cbSDH.SelectedItem.ToString()))
            {
                if (findCaseNum.ShowDialog(this) == DialogResult.OK)
                {
                    this.lbCaseArea.Text = string.Empty;
                    this.lbInstitution.Text = string.Empty;
                    this.lbEdction.Text = string.Empty;
                    this.lbLeglAspcts.Text = string.Empty;
                    this.lbEcoAspcts.Text = string.Empty;
                    this.lbtitle.Text = string.Empty;

                    this.casePartsSelected = string.Empty;
                    this.caseParts = string.Empty;
                    this.lbCasePartsSelected.Text = string.Empty;

                    string readers = string.Empty;

                    this.tbCaseNumTxt = findCaseNum.selectedRow[0].ToString();
                    this.lbCaseSubjectTxt = findCaseNum.selectedRow[1].ToString();
                    this.lbtitle.Text = findCaseNum.selectedRow[1].ToString();

                    List<Array> variant1Emneord = ConvergensDominoConnector.DominoConnector.Default.GetVariant1Emneord(this.cbSDH.Text, findCaseNum.selectedRow[2].ToString());

                    Array temp0 = variant1Emneord[0];
                    Array temp1 = variant1Emneord[1];
                    Array temp2 = variant1Emneord[2];
                    Array temp3 = variant1Emneord[3];
                    Array temp4 = variant1Emneord[4];
                    Array temp5 = variant1Emneord[7];
                    Array temp6 = variant1Emneord[8];

                    if (!this.tbCaseNumTxt.Equals(string.Empty))
                    {
                        this.btnCaseParts.Visible = true;

                        for (int i = 0; i < temp6.Length; i++)
                        {
                            if (i == 0)
                            {
                                this.caseParts += (string)temp6.GetValue(i);

                            }
                            else
                            {
                                this.caseParts += ";" + (string)temp6.GetValue(i);
                            }
                        }
                    }

                    this.lbtitle.Text = (string)variant1Emneord[5].GetValue(0);

                    #region // if protected
                    string caseprotected = (string)variant1Emneord[6].GetValue(0);

                    if (caseprotected.Equals("1"))
                    {
                        this.btnMailRights.Visible = false;

                        for (int i = 0; i < temp5.Length; i++)
                        {
                            if (i == 0)
                            {
                                readers += (string)temp5.GetValue(i);

                            }
                            else
                            {
                                readers += ";" + (string)temp5.GetValue(i);
                            }
                        }

                        this.setMailRightsTpTxt(readers);
                    }
                    else
                    {
                        this.btnMailRights.Visible = true;
                        this.setMailRightsTpTxt((string)variant1Emneord[7].GetValue(0));
                        //JAS Her kunne "hvem må se"-valglisten også sættes til "UVM" (som er default på sagen), men det er fint den ikke er på.
                    }
                    #endregion

                    for (int i = 0; i < temp0.Length; i++)
                    {
                        if (i == 0)
                        {
                            this.lbCaseArea.Text += (string)temp0.GetValue(i);

                        }
                        else
                        {
                            this.lbCaseArea.Text += ";" + (string)temp0.GetValue(i);
                        }
                    }


                    for (int i = 0; i < temp1.Length; i++)
                    {
                        if (i == 0)
                        {
                            this.lbEdction.Text += (string)temp1.GetValue(i);

                        }
                        else
                        {
                            this.lbEdction.Text += ";" + (string)temp1.GetValue(i);
                        }
                    }

                    for (int i = 0; i < temp2.Length; i++)
                    {
                        if (i == 0)
                        {
                            this.lbInstitution.Text += (string)temp2.GetValue(i);

                        }
                        else
                        {
                            this.lbInstitution.Text += ";" + (string)temp2.GetValue(i);
                        }
                    }


                    for (int i = 0; i < temp3.Length; i++)
                    {
                        if (i == 0)
                        {
                            this.lbLeglAspcts.Text += (string)temp3.GetValue(i);

                        }
                        else
                        {
                            this.lbLeglAspcts.Text += ";" + (string)temp3.GetValue(i);
                        }
                    }

                    for (int i = 0; i < temp4.Length; i++)
                    {
                        if (i == 0)
                        {
                            this.lbEcoAspcts.Text += (string)temp4.GetValue(i);

                        }
                        else
                        {
                            this.lbEcoAspcts.Text += ";" + (string)temp4.GetValue(i);
                        }
                    }

                    temp0 = null;
                    temp1 = null;
                    temp2 = null;
                    temp3 = null;
                    temp4 = null;
                    temp5 = null;
                    temp6 = null;

                    this.setCaseAreaTpTxt();
                    this.setEdctionTpTxt();
                    this.setInstitutionTpTxt();
                    this.setLeglAspctsTpTxt();
                    this.setEcoAspctsTpTxt();
                }
            }
        }

        private void btnFndRlpySeqNum_Click(object sender, EventArgs e)
        {
            using (FindRlpSeqNum fRlpSeqNum = new FindRlpSeqNum(this.cbSDH.Text, this.tbCaseNum.Text))
            {
                if (fRlpSeqNum.ShowDialog(this) == DialogResult.OK)
                {
                    this.tbSeqNumTxt = fRlpSeqNum.selectedRow[0].ToString();
                    //this.lbCaseSubjectTxt = fRlpSeqNum.selectedRow[1].ToString();
                    //move to a new label
                    //this.lbtitle.Text = fRlpSeqNum.selectedRow[1].ToString();
                }
            }
        }

        private void btnExpandCaseArea_Click(object sender, EventArgs e)
        {
            if (this.caseAreaSelected != string.Empty)
            {
                this.setCaseAreaTpTxt(this.caseAreaSelected);
            }
            else
            {
                this.setCaseAreaTpTxt();
            }
        }

        private void btnExpandEdction_Click(object sender, EventArgs e)
        {
            this.setEdctionTpTxt();
        }

        private void btnExpandInstitution_Click(object sender, EventArgs e)
        {
            this.setInstitutionTpTxt();
        }

        private void btnExpandLeglAspcts_Click(object sender, EventArgs e)
        {
            this.setLeglAspctsTpTxt();
        }

        private void btnExpandEcoAspcts_Click(object sender, EventArgs e)
        {
            this.setEcoAspctsTpTxt();
        }

        private void btnFreeCat_Click(object sender, EventArgs e)
        {
            using (FrmListbox fListBox = new FrmListbox("Frie kategorier", this.freeCatsSelected.Split(';'), this.cbSDH.Text))
            {
                //this.freeCatsSeleected = string.Empty;

                if (fListBox.ShowDialog(this) == DialogResult.OK)
                {
                    for (int i = 0; i < fListBox.listView.CheckedItems.Count; i++)
                    {
                        if (i == 0)
                        {
                            this.freeCatsSelected = fListBox.listView.CheckedItems[i].Text;
                        }
                        else
                        {
                            this.freeCatsSelected = this.freeCatsSelected + ";" + fListBox.listView.CheckedItems[i].Text;
                        }
                    }

                    this.setFreeCatsTpTxt(this.freeCatsSelected);
                }
            }
        }

        private void btnExpandFreeCats_Click(object sender, EventArgs e)
        {
            this.setFreeCatsTpTxt(this.freeCatsSelected);
        }

        private void tbCaseNum_Leave(object sender, EventArgs e)
        {
            #region Fill stuff from typed case number
            try
            {
                //060.38P.231
                this.lbCaseArea.Text = string.Empty;
                this.lbEdction.Text = string.Empty;
                this.lbInstitution.Text = string.Empty;
                this.lbLeglAspcts.Text = string.Empty;
                this.lbEcoAspcts.Text = string.Empty;
                this.lbtitle.Text = string.Empty;

                this.casePartsSelected = string.Empty;
                this.caseParts = string.Empty;
                this.btnCaseParts.Visible = false;
                this.lbCasePartsSelected.Text = string.Empty;

                string readers = string.Empty;

                string caseDocUnid = ConvergensDominoConnector.DominoConnector.Default.GetSDHCaseDocumentUNIDByCaseNumber(this.tbCaseNum.Text, this.cbSDH.Text);

                if (caseDocUnid != string.Empty)
                {
                    this.btnCaseParts.Visible = true;

                    List<Array> variant1Emneord = ConvergensDominoConnector.DominoConnector.Default.GetVariant1Emneord(this.cbSDH.Text, caseDocUnid);

                    Array temp0 = variant1Emneord[0];
                    Array temp1 = variant1Emneord[1];
                    Array temp2 = variant1Emneord[2];
                    Array temp3 = variant1Emneord[3];
                    Array temp4 = variant1Emneord[4];
                    Array temp5 = variant1Emneord[7];
                    Array temp6 = variant1Emneord[8];


                    #region // if protected
                    string caseprotected = (string)variant1Emneord[6].GetValue(0);

                    if (caseprotected.Equals("1"))
                    {
                        this.btnMailRights.Visible = false;

                        for (int i = 0; i < temp5.Length; i++)
                        {
                            if (i == 0)
                            {
                                readers += (string)temp5.GetValue(i);

                            }
                            else
                            {
                                readers += ";" + (string)temp5.GetValue(i);
                            }
                        }

                        this.setMailRightsTpTxt(readers);
                    }
                    #endregion

                    this.lbtitle.Text = (string)variant1Emneord[5].GetValue(0);

                    for (int i = 0; i < temp0.Length; i++)
                    {
                        if (i == 0)
                        {
                            this.lbCaseArea.Text += (string)temp0.GetValue(i);

                        }
                        else
                        {
                            this.lbCaseArea.Text += ";" + (string)temp0.GetValue(i);
                        }
                    }


                    for (int i = 0; i < temp1.Length; i++)
                    {
                        if (i == 0)
                        {
                            this.lbEdction.Text += (string)temp1.GetValue(i);

                        }
                        else
                        {
                            this.lbEdction.Text += ";" + (string)temp1.GetValue(i);
                        }
                    }

                    for (int i = 0; i < temp2.Length; i++)
                    {
                        if (i == 0)
                        {
                            this.lbInstitution.Text += (string)temp2.GetValue(i);

                        }
                        else
                        {
                            this.lbInstitution.Text += ";" + (string)temp2.GetValue(i);
                        }
                    }


                    for (int i = 0; i < temp3.Length; i++)
                    {
                        if (i == 0)
                        {
                            this.lbLeglAspcts.Text += (string)temp3.GetValue(i);

                        }
                        else
                        {
                            this.lbLeglAspcts.Text += ";" + (string)temp3.GetValue(i);
                        }
                    }

                    for (int i = 0; i < temp4.Length; i++)
                    {
                        if (i == 0)
                        {
                            this.lbEcoAspcts.Text += (string)temp4.GetValue(i);

                        }
                        else
                        {
                            this.lbEcoAspcts.Text += ";" + (string)temp4.GetValue(i);
                        }
                    }

                    for (int i = 0; i < temp6.Length; i++)
                    {
                        if (i == 0)
                        {
                            this.caseParts += (string)temp6.GetValue(i);

                        }
                        else
                        {
                            this.caseParts += ";" + (string)temp6.GetValue(i);
                        }
                    }

                    temp0 = null;
                    temp1 = null;
                    temp2 = null;
                    temp3 = null;
                    temp4 = null;
                    temp5 = null;
                    temp6 = null;

                    this.setCaseAreaTpTxt();
                    this.setEdctionTpTxt();
                    this.setInstitutionTpTxt();
                    this.setLeglAspctsTpTxt();
                    this.setEcoAspctsTpTxt();
                }
                else
                {
                    if (this.tbCaseNum.Text != string.Empty)
                    {
                        this.lbCaseArea.Text = string.Empty;
                        this.lbEdction.Text = string.Empty;
                        this.lbInstitution.Text = string.Empty;
                        this.lbLeglAspcts.Text = string.Empty;
                        this.lbEcoAspcts.Text = string.Empty;
                        this.lbtitle.Text = string.Empty;

                        MessageBox.Show("Kunne ikke finde sagen med sagsnummer " + this.tbCaseNum.Text + " i databasen " + this.cbSDH.SelectedValue, "Convergens Domino Connector for SDH", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }

                if (this.tbCaseNum.Text.Length == 0 & this.tbCaseNum.Text == string.Empty)
                {
                    this.lbCaseArea.Text = string.Empty;
                    this.lbEdction.Text = string.Empty;
                    this.lbInstitution.Text = string.Empty;
                    this.lbLeglAspcts.Text = string.Empty;
                    this.lbEcoAspcts.Text = string.Empty;
                    this.lbtitle.Text = string.Empty;
                }
            }
            catch (Exception)
            {
                
                throw;
            }

#endregion
        }

        private void tbSeqNum_Leave(object sender, EventArgs e)
        {
            try
            {
                //060.38P.231
                this.lbCaseArea.Text = string.Empty;
                this.lbEdction.Text = string.Empty;
                this.lbInstitution.Text = string.Empty;
                this.lbLeglAspcts.Text = string.Empty;
                this.lbEcoAspcts.Text = string.Empty;
                this.lbtitle.Text = string.Empty;

                this.btnCaseParts.Visible = false;
                this.casePartsSelected = string.Empty;
                this.caseParts = string.Empty;
                this.lbCasePartsSelected.Text = string.Empty;

                string readers = string.Empty;

                string caseDocUnid = ConvergensDominoConnector.DominoConnector.Default.GetSDHCaseDocumentUNIDByCaseNumber(this.tbCaseNum.Text, this.cbSDH.Text);

                if (caseDocUnid != string.Empty)
                {
                    this.btnCaseParts.Visible = true;

                    List<Array> variant1Emneord = ConvergensDominoConnector.DominoConnector.Default.GetVariant1Emneord(this.cbSDH.Text, caseDocUnid);

                    Array temp0 = variant1Emneord[0];
                    Array temp1 = variant1Emneord[1];
                    Array temp2 = variant1Emneord[2];
                    Array temp3 = variant1Emneord[3];
                    Array temp4 = variant1Emneord[4];
                    Array temp5 = variant1Emneord[7];
                    Array temp6 = variant1Emneord[8];

                    #region // if protected
                    string caseprotected = (string)variant1Emneord[6].GetValue(0);

                    if (caseprotected.Equals("1"))
                    {
                        this.btnMailRights.Visible = false;

                        for (int i = 0; i < temp5.Length; i++)
                        {
                            if (i == 0)
                            {
                                readers += (string)temp5.GetValue(i);

                            }
                            else
                            {
                                readers += ";" + (string)temp5.GetValue(i);
                            }
                        }

                        this.setMailRightsTpTxt(readers);
                    }
                    #endregion

                    this.lbtitle.Text = (string)variant1Emneord[5].GetValue(0);

                    for (int i = 0; i < temp0.Length; i++)
                    {
                        if (i == 0)
                        {
                            this.lbCaseArea.Text += (string)temp0.GetValue(i);

                        }
                        else
                        {
                            this.lbCaseArea.Text += ";" + (string)temp0.GetValue(i);
                        }
                    }


                    for (int i = 0; i < temp1.Length; i++)
                    {
                        if (i == 0)
                        {
                            this.lbEdction.Text += (string)temp1.GetValue(i);

                        }
                        else
                        {
                            this.lbEdction.Text += ";" + (string)temp1.GetValue(i);
                        }
                    }

                    for (int i = 0; i < temp2.Length; i++)
                    {
                        if (i == 0)
                        {
                            this.lbInstitution.Text += (string)temp2.GetValue(i);

                        }
                        else
                        {
                            this.lbInstitution.Text += ";" + (string)temp2.GetValue(i);
                        }
                    }


                    for (int i = 0; i < temp3.Length; i++)
                    {
                        if (i == 0)
                        {
                            this.lbLeglAspcts.Text += (string)temp3.GetValue(i);

                        }
                        else
                        {
                            this.lbLeglAspcts.Text += ";" + (string)temp3.GetValue(i);
                        }
                    }

                    for (int i = 0; i < temp4.Length; i++)
                    {
                        if (i == 0)
                        {
                            this.lbEcoAspcts.Text += (string)temp4.GetValue(i);

                        }
                        else
                        {
                            this.lbEcoAspcts.Text += ";" + (string)temp4.GetValue(i);
                        }
                    }

                    for (int i = 0; i < temp6.Length; i++)
                    {
                        if (i == 0)
                        {
                            this.caseParts += (string)temp6.GetValue(i);

                        }
                        else
                        {
                            this.caseParts += ";" + (string)temp6.GetValue(i);
                        }
                    }

                    temp0 = null;
                    temp1 = null;
                    temp2 = null;
                    temp3 = null;
                    temp4 = null;
                    temp5 = null;
                    temp6 = null;

                    this.setCaseAreaTpTxt();
                    this.setEdctionTpTxt();
                    this.setInstitutionTpTxt();
                    this.setLeglAspctsTpTxt();
                    this.setEcoAspctsTpTxt();
                }
                else
                {
                    if (this.tbCaseNum.Text != string.Empty)
                    {
                        this.lbCaseArea.Text = string.Empty;
                        this.lbEdction.Text = string.Empty;
                        this.lbInstitution.Text = string.Empty;
                        this.lbLeglAspcts.Text = string.Empty;
                        this.lbEcoAspcts.Text = string.Empty;
                        this.lbtitle.Text = string.Empty;

                        MessageBox.Show("Kunne ikke finde sagen med sagsnummer " + this.tbCaseNum.Text + " i databasen " + this.cbSDH.SelectedValue, "Convergens Domino Connector for SDH", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }

                if (this.tbCaseNum.Text.Length == 0 & this.tbCaseNum.Text == string.Empty)
                {
                    this.lbCaseArea.Text = string.Empty;
                    this.lbEdction.Text = string.Empty;
                    this.lbInstitution.Text = string.Empty;
                    this.lbLeglAspcts.Text = string.Empty;
                    this.lbEcoAspcts.Text = string.Empty;
                    this.lbtitle.Text = string.Empty;
                }
            }
            catch (Exception)
            {
                
                throw;
            }


        }

        private void tbDocName_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            e.IsInputKey = e.KeyCode == Keys.Return || e.KeyCode == Keys.Enter;
        }

        private void tbRemarks_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            e.IsInputKey = e.KeyCode == Keys.Return || e.KeyCode == Keys.Enter;
        }

        private void btnGetCaseArea_Click(object sender, EventArgs e)
        {
            using (FrmListbox fListBox = new FrmListbox("Vælg Sagsområde", this.caseAreaSelected.Split(';'), this.cbSDH.Text))
            {
                //this.caseAreaSeleected = string.Empty;

                if (fListBox.ShowDialog(this) == DialogResult.OK)
                {
                    for (int i = 0; i < fListBox.listView.CheckedItems.Count; i++)
                    {
                        if (i == 0)
                        {
                            this.caseAreaSelected = fListBox.listView.CheckedItems[i].Text;
                        }
                        else
                        {
                            this.caseAreaSelected = this.caseAreaSelected + ";" + fListBox.listView.CheckedItems[i].Text;
                        }
                    }
                }

                this.setCaseAreaTpTxt(this.caseAreaSelected);
            }

        }

        private void JournalizeSingle_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                DialogResult result = MessageBox.Show("Vil du afbryde journaliseringen?", "Convergens Domino Connector for SDH", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);

                if (result == DialogResult.OK)
                {
                    this.DialogResult = DialogResult.Cancel;
                }
            }
        }

        private void btnCaseParts_Click(object sender, EventArgs e)
        {
            using (FrmListbox fListBox = new FrmListbox("Vælg Sagsdele", this.casePartsSelected.Split(';'), this.caseParts.Split(';'), this.cbSDH.Text))
            {

                if (fListBox.ShowDialog(this) == DialogResult.OK)
                {
                    this.casePartsSelected = string.Empty;

                    for (int i = 0; i < fListBox.listView.CheckedItems.Count; i++)
                    {
                        if (i == 0)
                        {
                            this.casePartsSelected = fListBox.listView.CheckedItems[i].Text;
                        }
                        else
                        {
                            this.casePartsSelected = this.casePartsSelected + ";" + fListBox.listView.CheckedItems[i].Text;
                        }
                    }

                    this.lbCasePartsSelected.Text = casePartsSelected;

                    //this.setCasePartsTpTxt(this.casePartsSelected);
                }
            }

        }
    }

}
